package nl.cmkprojects.sage.slimplayer;

import java.util.Timer;
import java.util.TimerTask;

public class AutoTransitionTimerTask {
	private static final String DELAY_DEFAULT = "3000";
	private static final String AUTO_TRANSITION_DELAY = "slimplayer/transitionDelay";
	private boolean enabled;
	private String uiContext;
	private String sourceMenu;
	private String nextMenu;
	private Timer timer;
	private TimerTask timerTask;

	public AutoTransitionTimerTask(String uiContext, String sourceMenu,
			String nextMenu, boolean enabled) {
		super();
		this.uiContext = uiContext;
		this.sourceMenu = sourceMenu;
		this.nextMenu = nextMenu;
		this.enabled = enabled;
		this.timer = new Timer();
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public String getUiContext() {
		return uiContext;
	}

	public void setUiContext(String uiContext) {
		this.uiContext = uiContext;
	}

	public String getSourceMenu() {
		return sourceMenu;
	}

	public void setSourceMenu(String sourceMenu) {
		this.sourceMenu = sourceMenu;
	}

	public String getNextMenu() {
		return nextMenu;
	}

	public void setNextMenu(String nextMenu) {
		this.nextMenu = nextMenu;
	}

	public void start() {
		cancel();
		if (enabled) {
			long delay = Long.parseLong(SagePropertyHelper.getSageTvProperty(
					AUTO_TRANSITION_DELAY, DELAY_DEFAULT));
			timerTask = new myTimerTask();
			timer.schedule(timerTask, delay);
		}
	}

	public void cancel() {
		if (timerTask != null)
		{
			timerTask.cancel();
		}
	}

	private class myTimerTask extends TimerTask {
		@Override
		public void run() {
			SageTVApi.makeMenuTransition(uiContext, sourceMenu, nextMenu);
		}
	}

}
